package sjtu.ist.warehouse4ws.action;

import java.util.ArrayList;
import java.util.List;



import sjtu.ist.warehouse4ws.model.Domainmodel;
import sjtu.ist.warehouse4ws.model.Functionpoint;
import sjtu.ist.warehouse4ws.model.Process;

import sjtu.ist.warehouse4ws.service.DomainModelService;
import sjtu.ist.warehouse4ws.service.FunctionpointService;
import sjtu.ist.warehouse4ws.service.ProcessService;

import com.opensymphony.xwork2.ActionSupport;

public class TreeAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1180403231565653809L;
	private List <Domainmodel> domainModelList=new ArrayList<Domainmodel> ();
	private DomainModelService domainModelService;
	private String CnNameNode=null;
	
	private List<Process> processList=new ArrayList<Process>();
	private ProcessService processService;
	private String processNode=null;
	
	private List<Functionpoint> functionList=new ArrayList<Functionpoint>();
	private FunctionpointService functionpointService;
	private String functionNode=null;
	
	

	public void setDomainModelService(DomainModelService domainModelService) {
		this.domainModelService = domainModelService;
	}

	public void setProcessService(ProcessService processService){
		this.processService=processService;
	}
	
	public void setFunctionpointService(FunctionpointService functionpointService){
		this.functionpointService=functionpointService;
	}
	
	public String showDomainTreeNode(){
		    this.domainModelList=domainModelService.getDomainModelList();
		    CnNameNode=showBusinessObject();
			return SUCCESS;								
			
			}
	
	public String showProcessTreeNode(){
		this.processList=processService.getProcessList();
		processNode=showProcess();
		return SUCCESS;
	}
	
	public String showFunctionTreeNode(){
		this.functionList=functionpointService.getFunctionpointList();
		functionNode=showFunction();
		return SUCCESS;
	}
	public String showBusinessObject(){
		StringBuffer cnamejson = new StringBuffer();
		cnamejson.append("[");
		for(Domainmodel domain:domainModelList){		
			cnamejson.append("{text:'");
			cnamejson.append(domain.getCnName());
			cnamejson.append("'");
			cnamejson.append(",id:'");
			cnamejson.append(domain.getId());
			cnamejson.append("'");
			cnamejson.append(",leaf:true },");
			
		}
		cnamejson.deleteCharAt(cnamejson.length()-1);
		cnamejson.append("]");
	//	System.out.print(cnamejson.toString());
		return cnamejson.toString();
	}
	
	public String showProcess(){
		StringBuffer processjson = new StringBuffer();
		processjson.append("[");
		for(Process process:processList){
			processjson.append("{text:'");
			processjson.append(process.getCnName());
			processjson.append("'");
			processjson.append(",id:'");
			processjson.append(process.getId());
			processjson.append("'");
			processjson.append(",leaf:true },");
			
		}
		processjson.deleteCharAt(processjson.length()-1);
		processjson.append("]");
		System.out.print(processjson.toString());
		return processjson.toString();
	}
	
	public String showFunction(){
		StringBuffer processjson = new StringBuffer();
		processjson.append("[");
		for(Functionpoint function:functionList){
			processjson.append("{text:'");
			processjson.append(function.getCnName());
			processjson.append("'");
			processjson.append(",id:'");
			processjson.append(function.getId());
			processjson.append("'");
			processjson.append(",leaf:true },");
			
		}
		processjson.deleteCharAt(processjson.length()-1);
		processjson.append("]");
		System.out.print(processjson.toString());
		return processjson.toString();
	}
	////////////////////////////////
	public String getCnNameNode() {
		return CnNameNode;
	}

	public void setCnNameNode(String cnNameNode) {
		CnNameNode = cnNameNode;
	}

	public String getProcessNode() {
		return processNode;
	}

	public void setProcessNode(String processNode) {
		this.processNode = processNode;
	}

	public String getFunctionNode() {
		return functionNode;
	}

	public void setFunctionNode(String functionNode) {
		this.functionNode = functionNode;
	}

	/////////////////////////////////////////
    
    

}
